$(document).ready(function(){
	var skin_skin='';
	var skin_smskin='';
	var modify_skin='';
	var modify_smskin='';
	var page=1;
	//删除皮肤操作
	 $(".del").click(function(event){
    	event.stopPropagation(); 
    	var skinid = $(this).data('skinid');
		swal ({
		  title: "你确定要删除改皮肤嘛？" ,
		  text: "一旦删除，你将无法恢复这个文件！" ,
		  type: "info" ,
		  showCancelButton: true ,
		  confirmButtonColor: "#DD6B55",
		  confirmButtonText: "确定",
		  closeOnConfirm: false ,
		  showLoaderOnConfirm: true ,
		},
		function (){ 
			
			function GetQueryString(name)
			{
			     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			     var r = window.location.search.substr(1).match(reg);
			     if(r!=null)return  unescape(r[2]); return null;
			}
			page = GetQueryString("page");
			if(page == null || page == undefined){
				page=1;
			}
			$.ajax({
		            url: 'index.php?c=skin&a=deleteskin',
		            type: 'get',
		            dataType: 'json',
		            data: {'skinid':skinid,'page':page}
		       	}).done(function (data) {
		            if(data.code==1){
						  setTimeout(function () {
                                swal({
                                    title: "删除成功!",
                                    type: "success"
                                }, function () {
                                   window.location.href = "index.php?c=skin&a=skindata&page="+data.page;
                                });
                            }, 1000);
		               
		            }else{
		            	
		            	 setTimeout(function () {
                                swal({
                                    title: "删除失败!",
                                    type: "error"
                                }, function () {
                                     location.reload();
                                });
                            }, 1000);
		            }
		            })	
		            .fail(function (error) {
		                console.log(error);
		            });
		});
  	});
  	
  	//点击头像上传文件
			$('.skin_skin').on('click',function(){
				$('#file').trigger('click');
			});
			
  	//选择图片之后
			$('#file').on('change',function(){
				var file = $('#file')[0].files[0];//获取文件都是数组
				var reader = new FileReader();
				//解析完之后进行的函数调用
				reader.onload = function(event){
					//解析的目标结果
					var dataurl= event.target.result;
					//进行页面显示海报的图片
					$(".skin_skin").attr("src",dataurl);
					skin_skin=dataurl;
				}
				//相当于解析图片
				reader.readAsDataURL(file);
				
			});
			
			$('.skin_smskin').on('click',function(){
				$('#smfile').trigger('click');
			});
			$('#smfile').on('change',function(){
				var smfile = $('#smfile')[0].files[0];//获取文件都是数组
				var reader = new FileReader();
				//解析完之后进行的函数调用
				reader.onload = function(event){
					//解析的目标结果
					var dataurl= event.target.result;
					$(".skin_smskin").attr("src",dataurl);
					skin_smskin=dataurl;
				}
				//相当于解析图片
				reader.readAsDataURL(smfile);
				
			});
	
  	//添加皮肤操作
  	$('.add').click(function(){
    	var skin_id = $('.skin_id').val();
    	var skin_name = $('.skin_name').val();
    	var skin_price = $('.skin_price').val();
		if (skin_skin === undefined || skin_skin.replace(/\s+/g, "") === "") {
				 swal("请添加英雄皮肤图片", "", "info"); 
		        } else if(skin_smskin === undefined || skin_smskin.replace(/\s+/g, "") === ""){
		        	swal("请添加英雄皮肤缩略图", "", "info"); 
				} else if(skin_id === undefined || skin_id.replace(/\s+/g, "") === ""){
		        	swal("请添加英雄id", "", "info"); 
				} else if(skin_name === undefined || skin_name.replace(/\s+/g, "") === ""){
		        	swal("请添加英雄皮肤名称", "", "info"); 
				} else if(skin_price === undefined || skin_price.replace(/\s+/g, "") === ""){
		        	swal("请添加英雄皮肤价格", "", "info"); 
				}else{
					//进行参数传递
						var file = $('#file')[0].files[0];
						var smfile = $('#smfile')[0].files[0];
						var formdata =new FormData();
						formdata.append('file',file);
						formdata.append('smfile',smfile);
						formdata.append('skin_id',skin_id);
						formdata.append('skin_name',skin_name);
						formdata.append('skin_price',skin_price);

						$.ajax({
							url: 'index.php?c=skin&a=addskin',
							type: 'post',
							dataType: 'json',
							processData:false,
							contentType:false,
							data: formdata,
						})
						.done(function(data) {
						if(data.code==1){

                                swal({
                                    title: "添加成功!",
                                    type: "success"
                                }, function () {
                                   window.location.href = "index.php?c=skin&a=skindata&page="+data.page;
                                });
								
							}else {
								swal("添加失败",data.msg, "error"); 
							}
						})
						.fail(function(error) {
							console.log(error);
						});
				}
		});
		
		
		
	//点击头像上传文件
			$('.modify_skin').on('click',function(){
				$('#modify').trigger('click');
			});
	//选择图片之后
			$('#modify').on('change',function(){
				var file = $('#modify')[0].files[0];//获取文件都是数组
				var reader = new FileReader();
				//解析完之后进行的函数调用
				reader.onload = function(event){
					//解析的目标结果
					var dataurl= event.target.result;
					//进行页面显示海报的图片
					$(".modify_skin").attr("src",dataurl);
					modify_skin=dataurl;
				}
				//相当于解析图片
				reader.readAsDataURL(file);
				
			});
			
			
			
			$('.modify_smskin').on('click',function(){
				$('#smmodify').trigger('click');
			});
			$('#smmodify').on('change',function(){
				var smmodify = $('#smmodify')[0].files[0];//获取文件都是数组
				var reader = new FileReader();
				//解析完之后进行的函数调用
				reader.onload = function(event){
					//解析的目标结果
					var dataurl= event.target.result;
					$(".modify_smskin").attr("src",dataurl);
					smmodify=dataurl;
				}
				//相当于解析图片
				reader.readAsDataURL(smmodify);
				
			});
	//修改皮肤操作
  	$('.modify').click(function(event){
  		var skin_id = $(this).data('skinid');
    	var modify_id = $('#'+skin_id+' .modify_id').val();
    	var modify_name = $('#'+skin_id+' .modify_name').val();
    	var modify_price = $('#'+skin_id+' .modify_price').val();
//  	console.log(skin_id,modify_id,modify_name,modify_price);
		if(modify_id === undefined || modify_id.replace(/\s+/g, "") === ""){
		        	swal("请填写英雄id", "", "info"); 
				} else if(modify_name === undefined || modify_name.replace(/\s+/g, "") === ""){
		        	swal("请填写英雄皮肤名称", "", "info"); 
				} else if(modify_price === undefined || modify_price.replace(/\s+/g, "") === ""){
		        	swal("请填写英雄皮肤价格", "", "info"); 
				}else{
					//如果有图片传入 	
						if(modify_skin!=undefined && modify_smskin!=undefined){
							//进行参数传递
								var file = $('#modify')[0].files[0];
								var smmodify = $('#smmodify')[0].files[0];
								var formdata =new FormData();
								formdata.append('file',file);
								formdata.append('smmodify',smmodify);
								formdata.append('skin_id',skin_id);
								formdata.append('modify_id',modify_id);
								formdata.append('modify_name',modify_name);
								formdata.append('modify_price',modify_price);
		
								$.ajax({
									url: 'index.php?c=skin&a=modifyskin',
									type: 'post',
									dataType: 'json',
									processData:false,
									contentType:false,
									data: formdata,
								})
								.done(function(data) {
								if(data.code==1){
										swal("修改成功", "", "success");	
										setTimeout(function(){
											window.location.reload();
										},1000);	
									}else {
										swal("修改失败",data.msg, "error"); 
									}
								})
								.fail(function(error) {
									console.log(error);
								});
						}else if(modify_skin!=undefined && modify_smskin==undefined){
								//进行参数传递
								var file = $('#modify')[0].files[0];
								var formdata =new FormData();
								formdata.append('file',file);
								formdata.append('skin_id',skin_id);
								formdata.append('modify_id',modify_id);
								formdata.append('modify_name',modify_name);
								formdata.append('modify_price',modify_price);
		
								$.ajax({
									url: 'index.php?c=skin&a=modifyskin',
									type: 'post',
									dataType: 'json',
									processData:false,
									contentType:false,
									data: formdata,
								})
								.done(function(data) {
								if(data.code==1){
										swal("修改成功", "", "success");	
										setTimeout(function(){
											window.location.reload();
										},1000);	
									}else {
										swal("修改失败",data.msg, "error"); 
									}
								})
								.fail(function(error) {
									console.log(error);
								});
						}else if(modify_skin==undefined && modify_smskin!=undefined){
								//进行参数传递
								var smmodify = $('#smmodify')[0].files[0];
								var formdata =new FormData();
								formdata.append('smmodify',smmodify);
								formdata.append('skin_id',skin_id);
								formdata.append('modify_id',modify_id);
								formdata.append('modify_name',modify_name);
								formdata.append('modify_price',modify_price);
		
								$.ajax({
									url: 'index.php?c=skin&a=modifyskin',
									type: 'post',
									dataType: 'json',
									processData:false,
									contentType:false,
									data: formdata,
								})
								.done(function(data) {
								if(data.code==1){
										swal("修改成功", "", "success");	
										setTimeout(function(){
											window.location.reload();
										},1000);	
									}else {
										swal("修改失败",data.msg, "error"); 
									}
								})
								.fail(function(error) {
									console.log(error);
								});
						}else{
						//如果没有图片传入
							$.ajax({
					            url: 'index.php?c=skin&a=modifyskin',
					            type: 'post',
					            dataType: 'json',
					            data: {'skin_id':skin_id,'modify_id':modify_id,'modify_name':modify_name,'modify_price':modify_price},
					       	}).done(function (data) {
					            if(data.code==1){
					            	 swal("修改成功", "", "success");	
										setTimeout(function(){
											window.location.reload();
										},1000);
					               
					            }else{
					            	swal("修改失败",data.msg, "error"); 
					            }
					            })	
					            .fail(function (error) {
					                console.log(error);
					            });
						}
				}
		});	
  	
});